--- 模块功能：MQTT客户端数据发送处理
-- @author openLuat
-- @module mqtt.mqttOutMsg
-- @license MIT
-- @copyright openLuat
-- @release 2018.03.28


module(..., package.seeall)
require "logGenerator"
require "earthquakeWaring"
--数据发送的消息队列
local msgQueue, sysTime, logInfo, slen = {}, "", "", string.len

function insertMsg(topic, payload, qos, user)
    table.insert(msgQueue, { t = topic, p = payload, q = qos, user = user })
    sys.publish("APP_SOCKET_SEND_DATA")
end

-- local function pubQos0TestCb(result)
--     log.info("mqttOutMsg.pubQos0TestCb",result)
--     if result then sys.timerStart(pubQos0Test,10000) end
-- end

-- function pubQos0Test()
--     insertMsg("/qos0topic","qos0data",0,{cb=pubQos0TestCb})
-- end

-- local function pubQos1TestCb(result)
--     log.info("mqttOutMsg.pubQos1TestCb",result)
--     if result then sys.timerStart(pubQos1Test,20000) end
-- end

-- function pubQos1Test()
--     insertMsg("/中文qos1topic","中文qos1data",1,{cb=pubQos1TestCb})
-- end

--- 初始化“MQTT客户端数据发送”
-- @return 无
-- @usage mqttOutMsg.init()
-- function init()
--     pubQos0Test()
--     pubQos1Test()
-- end

--- 去初始化“MQTT客户端数据发送”
-- @return 无
-- @usage mqttOutMsg.unInit()
-- function unInit()
--     sys.timerStop(pubQos0Test)
--     sys.timerStop(pubQos1Test)
--     while #msgQueue>0 do
--         local outMsg = table.remove(msgQueue,1)
--         if outMsg.user and outMsg.user.cb then outMsg.user.cb(false,outMsg.user.para) end
--     end
-- end


--- MQTT客户端数据发送处理
-- @param mqttClient，MQTT客户端对象
-- @return 处理成功返回true，处理出错返回false
-- @usage mqttOutMsg.proc(mqttClient)
function proc(mqttClient)
    while #msgQueue > 0 do
        local base64DecodeMsg, aesDecryptMsg
        sysTime = earthquakeWaring.GetSystemTime()
        local outMsg = table.remove(msgQueue, 1)
        log.info("msgQueue.P:",outMsg.p)
        local result = mqttClient:publish(outMsg.t, outMsg.p, outMsg.q)
        if "answer" == outMsg.t then
            sysTime = earthquakeWaring.GetSystemTime()
            logInfo = logInfo .. "[" .. sysTime .. "]" .. "烈度回传成功\n"
        elseif "action" == outMsg.t then
            base64DecodeMsg = crypto.base64_decode(outMsg.p, slen(outMsg.p))
            aesDecryptMsg = crypto.aes_decrypt("ECB", "PKCS5", base64DecodeMsg,
                "keson-123abcdefg")
            local tjsondata, resultMqtt, errinfo = json.decode(aesDecryptMsg)
            if resultMqtt and type(tjsondata) == "table" then
                local todo = tjsondata["todo"]
                if "writeConfig" == todo then
                    sysTime = earthquakeWaring.GetSystemTime()
                    logInfo = logInfo .. "[" .. sysTime .. "]" .. "设备参数配置成功\n"
                elseif "devStatus" == todo then
                    sysTime = earthquakeWaring.GetSystemTime()
                    logInfo = logInfo .. "[" .. sysTime .. "]" .. "设备参数状态查询成功\n"
                elseif "gps" == todo then
                    sysTime = earthquakeWaring.GetSystemTime()
                    logInfo = logInfo .. "[" .. sysTime .. "]" .. "设备位置查询成功\n"
                end
            end
        elseif "online" == outMsg.t then
            sysTime = earthquakeWaring.GetSystemTime()
            logInfo = logInfo .. "[" .. sysTime .. "]" .. "设备上/离线状态查询成功！\n"

        end
        logGenerator.generateLogTask(logInfo, "log")
        if outMsg.user and outMsg.user.cb then outMsg.user.cb(result, outMsg.user.para) end
        if not result then return end
    end
    return true
end
